package com.yanglx;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class I18nUtils {
    //文件输出位置
    public final static String FILE_PATH = "D://";

    public static void main(String[] args) {
        //当前所在菜单
        String htmlPath = "D:\\dev\\workspace\\greatsell_iu\\gs-portal\\src\\main\\resources\\templates\\wms\\pickingSo\\pickingSo.html";
        FileReader fileReader = new FileReader(htmlPath);
        String selectedText = fileReader.readString();
        //html里的国际化
        outExcelByLabel(selectedText);
    }

    public static void outExcelByLabel(String docText) {
        Set<String> set = new HashSet<>();
        List<List<String>> dataList = new ArrayList<>();
        Document document = Jsoup.parse(docText);
        Elements elementsByAttribute = document.getElementsByAttribute("th:text");
        Element first = elementsByAttribute.first();
        //国际化
        String thText = first.attr("th:text");
        if (thText == null || thText.length() == 0) {
            return;
        }
        String i18nCode = thText.replace("#{", "").replace("}", "");
        String currMenu = getCurrMenu(i18nCode);
        String web = getWeb(i18nCode);

        File file = new File(FILE_PATH + "/" + currMenu + ".xlsx");
        //最后一行，用于追加
        int lastRowNum = 1;
        if (file.exists()) {
            //文件存在则读取数据放到set用于过滤重复代码
            ExcelReader reader = ExcelUtil.getReader(file);
            List<Map<String, Object>> maps = reader.readAll();
            for (Map<String, Object> map : maps) {
                set.add((String) map.get("代码"));
            }
            lastRowNum = reader.getSheet().getLastRowNum();
        }

        //解析html
        for (Element element : elementsByAttribute) {
            //中文名
            String text = element.text();
            //国际化
            String attr = element.attr("th:text");
            if (StrUtil.isNotBlank(text)) {
                String i18n = attr.replace("#{", "").replace("}", "");
                if (!set.contains(i18n)) {
                    String[] split = i18n.split("_");
                    if (web == null) {
                        web = split[1].toUpperCase(Locale.ROOT);
                    }
                    if (currMenu == null) {
                        String[] strings = i18n.split("\\.");
                        currMenu = strings[0].replace(split[0] + "_" + split[1] + "_", "");
                    }
                    List<String> row = CollUtil.newArrayList(web, currMenu, i18n, text, "2", "3");
                    dataList.add(row);
                    set.add(i18n);
                }
            }
        }

        String pattern = "#\\{(\\.|\\S)*}";
        Pattern compile = Pattern.compile(pattern);
        Matcher matcher = compile.matcher(docText);
        while (matcher.find()) {
            String group1 = matcher.group();
            String i18nCode1 = group1.replace("#{", "").replace("}", "");
            if (!set.contains(i18nCode1)) {
                List<String> row = CollUtil.newArrayList(web, currMenu, i18nCode1, "", "2", "3");
                dataList.add(row);
                set.add(i18nCode1);
            }
        }

        ExcelWriter writer = ExcelUtil.getWriter(file, "i18n");
        writer.setColumnWidth(0, 25);
        writer.setColumnWidth(1, 25);
        writer.setColumnWidth(2, 25);
        writer.setColumnWidth(3, 25);
        writer.setColumnWidth(4, 25);
        writer.setColumnWidth(5, 25);
        if (lastRowNum == 1) {
            List<Object> title = CollUtil.newArrayList("国际化类型", "所在菜单", "代码", "名称 中文", "名称 英文", "名称 法文");
            writer.writeRow(title);
        }
        writer.setCurrentRow(lastRowNum);
        writer.write(dataList);
        writer.flush();
        writer.close();
    }


    public static String getCurrMenu(String i18nCode) {
        String[] split = i18nCode.split("_");
        String[] strings = i18nCode.split("\\.");
        return strings[0].replace(split[0] + "_" + split[1] + "_", "");
    }

    public static String getWeb(String i18nCode) {
        String[] split = i18nCode.split("_");
        return split[1].toUpperCase(Locale.ROOT);
    }

}
